package com.lanswon.qzsmk.util.pdfutil;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.LoggerFactory;

import com.lanswon.qzsmk.base.PathContants;

import ch.qos.logback.classic.Logger;

/**
 * 描述：生成打印凭证文件
 * 新建程序 版本v1.0
 * 新建时间：20180126
 * @author 惠国磊
 *
 */
public class TempletTicket {
	/* logger对象 */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(TempletTicket.class);
	
	/*各凭证的，模板文件名称的常量定义*/
	// 退卡登记，和退卡退款比较
	/* 申请,换卡,补卡登记凭证  temp1*/
	private final static String CREDENTIAL_TEMP_1 = "CREDENTIAL_TEMP1";
	/* 退卡登记凭证  temp2*/
	private final static String CREDENTIAL_TEMP_2 = "CREDENTIAL_TEMP2";
	/* 换卡,补卡发卡凭证 temp3*/
	private final static String CREDENTIAL_TEMP_3 = "CREDENTIAL_TEMP3";
	/* 正式挂失,挂失解挂，申请现场发卡，申请发卡，现场售卡 凭证 temp4*/
	private final static String CREDENTIAL_TEMP_4 = "CREDENTIAL_TEMP4";
	/* 现场退卡退款凭证 temp5*/
	private final static String CREDENTIAL_TEMP_5 = "CREDENTIAL_TEMP5";
	/* 退卡退款凭证 temp6*/
	private final static String CREDENTIAL_TEMP_6 = "CREDENTIAL_TEMP6";
	/* 电子钱包充值凭证 temp5*/
	private final static String CREDENTIAL_TEMP_7 = "CREDENTIAL_TEMP7";
	/* 批量发卡凭证 temp5*/
	private final static String CREDENTIAL_TEMP_8 = "CREDENTIAL_TEMP8";
	/* 批量申请凭证 temp5*/
	private final static String CREDENTIAL_TEMP_9 = "CREDENTIAL_TEMP9";
	/* 出入库库存 */
	private final static String STORAGE_TEMP_1 = "STORAGE_TEMP1";
	
	/*凭证pdf路径*/
	private final static String CARD_CREDENTIAL_PATH = "/qzsmk/resource/credentials/";
	
	/**
	 * changeAndReleaseIT
	 * @param String type 凭证类型
	 * @param String cardNo 市民卡号
	 * @throws Exception
	 */
	public String createPDF(Map<String,Object> workMap,HttpServletRequest request) {

		if(logger.isInfoEnabled()){
			logger.info("createPDF########start()");
		}
		/* 1.模板（空文件）路径 */
		String tempPath = "../../pdf_template/";
		/* 2.模板（文件）名称 */
		String tempFileName = null;
		/* 3.目标文件路径 */
//		String targetPath = "smb://commonuser:Lanswon2018@192.168.0.12/qzsmk/00.temple/";
//		String targetPath = "D:\\Apache24\\htdocs\\qzsmk\\resource\\credentials\\";
		HttpSession session =  request.getSession();
		String root =session.getServletContext().getRealPath("/");
		String targetPath = root + PathContants.PROJECT_PATH_CERT_IMPORTED;
		File dirFile = new File(targetPath);
		logger.debug("获取凭证路径3：【{}】",root);
		logger.debug("获取凭证路径1：【{}】",targetPath);
		logger.debug("获取凭证路径2：【{}】",PathContants.PROJECT_PATH_CERT_IMPORTED);
		if(!dirFile.exists()){
			dirFile.mkdirs();
		}
		/* 4.目标文件名称 */
		String targetFileName = null;

		// 判断前端画面传入的参数是否为空，如果为空或null,抛出异常
		/*if ("".equals(type) || null == type) {
			logger.error("凭证参数为空！");
			// throw new exception();
		}
		if("".equals(cardNo) || null == cardNo){
			logger.error("市民卡号为空！");
		}*/
		String type = (String)workMap.get("credential_type");
		if ("".equals(type) || null == type) {
			logger.error("凭证参数为空！");
			// throw new exception();
		}
		
		if("applyRegister".equals(type)){
			tempFileName = CREDENTIAL_TEMP_1+".pdf";
		}else if("applyRelease".equals(type)||"sellCard".equals(type)){
			tempFileName = CREDENTIAL_TEMP_2+".pdf";
		}else if("changeAndReleaseIT".equals(type)||"changeAndRelease".equals(type)||
				"supplierAndReleaseIT".equals(type)||"supplierAndRelease".equals(type)){
			tempFileName = CREDENTIAL_TEMP_3+".pdf";
		}else if("lossRegister".equals(type)||"lossCancel".equals(type)||"applyReleaseIT".equals(type)
				||"changeCardRegister".equals(type)||"supplierCardRegister".equals(type)){
			tempFileName = CREDENTIAL_TEMP_4+".pdf";
		}else if("openApp".equals(type)){
			tempFileName = CREDENTIAL_TEMP_5+".pdf";
		}else if("logoffCardRegister".equals(type)||"openAndCloseApp".equals(type)||"cancelRefund".equals(type)){
			tempFileName = CREDENTIAL_TEMP_6+".pdf";
		}else if("EPPay".equals(type)){
			tempFileName = CREDENTIAL_TEMP_7+".pdf";
		}else if("batchCard".equals(type)){
			tempFileName = CREDENTIAL_TEMP_8+".pdf";
		}else if("applyBatchRegister".equals(type)){
			tempFileName = CREDENTIAL_TEMP_9+".pdf";
		}
		
		logger.debug("tempFileName:"+tempFileName);
		String credentialNo = (String)workMap.get("credentialNo");
		targetFileName = type+credentialNo+"_print.pdf";
		if(logger.isDebugEnabled()){
			logger.debug("targetFileName:"+targetFileName);
		}
		Ticket ticket = new Ticket();
		
		/*凭证上的通用变量： 1.凭证号2.业务类型3.卡类型4.经办网点5.经办人6.办理日期
		 * 
		 * */
		//以下是测试代码，变量赋值写死
		//1.设定凭证号
		ticket.setCert_no(credentialNo);
		//2.设定卡类型
		ticket.setCard_type((String)workMap.get("cardType"));
		//3.设定经办网点
		ticket.setBranchNo((String)workMap.get("branchNo"));
		//4.设定经办人
		ticket.setOperatorNo((String)workMap.get("operatorNo"));
		//5.设定办理日期
		ticket.setOperateDate((String)workMap.get("operateDate"));

		/*以下设定的凭证信息，是判断业务类型设定*/
		//1.申请登记的自身变量
		if("applyRegister".equals(type)){
			//市民卡号的设定
			//ticket.setCard_no((String)workMap.get("cardNo"));
			//预约卡日期的设定
			//ticket.setCardAppointment((String)workMap.get("cardAppointment"));
			//卡产品的设定
			ticket.setCardpName((String)workMap.get("cardpName"));
			//业务类型的设定
				ticket.setBussiness_type((String)workMap.get("bussinessType"));
		}
		
		// 2.单笔发卡，不记名售卡的自身变量设定
		if("applyRelease".equals(type)||"sellCard".equals(type)){
			//工本费
			ticket.setCardDeposit((String)workMap.get("transactionAmount"));
			//新卡卡号设定
			ticket.setCard_no((String)workMap.get("newCardNo"));
			if("applyRelease".equals(type)){
				//业务类型设定
				ticket.setBussiness_type("单笔发卡");
			}else{
				//业务类型设定
				ticket.setBussiness_type("不记名卡售卡");
			}
		}
		
		// 3.换卡现场发卡，换卡发卡，补卡现场发卡，补卡发卡
		if("changeAndReleaseIT".equals(type)||"changeAndRelease".equals(type)||
			"supplierAndReleaseIT".equals(type)||"supplierAndRelease".equals(type)){
			//旧卡卡号设定
			ticket.setOld_card_no((String)workMap.get("oldCardNo"));
			//旧卡余额设定
			//ticket.setOld_card_balance("0.02");
			//新卡卡号设定
			ticket.setNew_card_no((String)workMap.get("newCardNo"));
			//新卡余额设定
			//ticket.setNew_card_balance("50.00");
			//押金
			ticket.setCardDeposit((String)workMap.get("transactionAmount"));
			//业务类型设定
			if("changeAndReleaseIT".equals(type)){
				ticket.setBussiness_type("换卡现场发卡");
			}else if("changeAndRelease".equals(type)){
				ticket.setBussiness_type("换卡发卡");
			}else if("supplierAndReleaseIT".equals(type)){
				ticket.setBussiness_type("补卡现场发卡");
			}else{
				ticket.setBussiness_type("补卡发卡");
			}
		}
		
		// 4.申请现场发卡，换卡登记，补卡登记，现场售卡，正式挂失，挂失解挂
		if("lossRegister".equals(type)||"lossCancel".equals(type)||"applyReleaseIT".equals(type)
				||"changeCardRegister".equals(type)||"supplierCardRegister".equals(type)){
			//市民卡号(实际需要后台传值)
			if("applyRelease".equals(type)){
			ticket.setCard_no((String)workMap.get("newCardNo"));
			}else{
			ticket.setCard_no((String)workMap.get("cardNo"));
			
			}
			//经办人(实际需要后台传值)
			//ticket.setOperatorNo((String)workMap.get("operatorNo"));
			///System.out.println("1111111111111");
			//业务类型
			if("lossRegister".equals(type)){
				ticket.setBussiness_type("挂失登记");
			}else if("lossCancel".equals(type)){
				ticket.setBussiness_type("挂失解挂");
			}else if("applyReleaseIT".equals(type)){
				ticket.setBussiness_type("申请现场发卡");
			}else if("changeCardRegister".equals(type)){
				ticket.setBussiness_type("换卡登记");
			}else if("supplierCardRegister".equals(type)){
				ticket.setBussiness_type("补卡登记");
			}
		}
		
		// 5.开通应用
		if("openApp".equals(type)){
			//市民卡号
			ticket.setCard_no((String)workMap.get("newCardNo"));
			//押金
			ticket.setCardDeposit((String)workMap.get("feeBln"));
			//总金额 
			ticket.setLogoffTotalMoney((String)workMap.get("transactionAmount"));
			//业务类型 
			ticket.setBussiness_type((String)workMap.get("bussinessType"));
		}
		
		// 6.关闭应用，联名卡注销退款，注销
		if("logoffCardRegister".equals(type)||"openAndCloseApp".equals(type)||"cancelRefund".equals(type)){
			//市民卡号
			ticket.setCard_no((String)workMap.get("newCardNo"));
			
			ticket.setCardDeposit((String)workMap.get("feeBln"));
			//退款总金额 
			ticket.setLogoffTotalMoney((String)workMap.get("transactionAmount"));
			//业务类型 
			ticket.setBussiness_type((String)workMap.get("bussinessType"));
			if("cancelRefund".equals(type)){
				//业务类型 
				ticket.setBussiness_type("联名卡注销退款凭证");
			}else if("logoffCardRegister".equals(type)){
				//市民卡号
				ticket.setCard_no((String)workMap.get("oldCardNo"));
			}
		}
		
		// 7.电子钱包充值
		if("EPPay".equals(type)){
			//市民卡号
			ticket.setCard_no((String)workMap.get("newCardNo"));
			//卡内余额
			ticket.setCurrent_balance((String)workMap.get("ariginalAmount"));
			//充入金额
			ticket.setReChargeMoney((String)workMap.get("transactionAmount"));
			//现有金额
			ticket.setAfterRecharge((String)workMap.get("currentAmount"));
			//办理时间
			ticket.setOperateTime((String)workMap.get("operateTime"));
			if("3F01".equals(workMap.get("appFlag"))){
			//业务类型
			ticket.setBussiness_type("公交车应用充值");
			}else if("M1BK".equals(workMap.get("appFlag"))){
				//业务类型
				ticket.setBussiness_type("自行车应用充值");	
			}
		}
		// 8.批量发卡
		if("batchCard".equals(type)){
			//单位名称
			ticket.setCorpName((String)workMap.get("corpName"));
			//数量
			ticket.setRecordNum((String)workMap.get("recordNum"));
			//卡产品
			ticket.setCardpName((String)workMap.get("cardpName"));
			//业务类型
			ticket.setBussiness_type("批量发卡");
		}
		
		// 9.批量申请
		if("applyBatchRegister".equals(type)){
			//工作单位
			ticket.setCorpName((String)workMap.get("corpName"));
			//卡产品
			ticket.setCardpName((String)workMap.get("cardpName"));
			//数量
			ticket.setRecordNum((String)workMap.get("recordNum"));
			//总金额
			ticket.setCardDeposit((String)workMap.get("transactionAmount"));
			//业务类型
			ticket.setBussiness_type("批量申请");
		}

		request.getSession().setAttribute("ticket", ticket);
		
		PDFTempletTicket pdfTT = new PDFTempletTicket();
		
		// 指定服务器路径，pdf文件的名称定义为每个流程凭证+是市民卡ha
		pdfTT.setTemplatePdfPath(tempPath+tempFileName);

		// 打印机上的服务
		pdfTT.setTargetPdfpath(targetPath+File.separator+targetFileName);
		logger.debug("pdfTT_path:"+pdfTT.getTargetPdfpath());
		pdfTT.setTicket(ticket);
		
		//创建远程pdf文件
		File file = new File(targetPath+File.separator+targetFileName);
		pdfTT.templetTicket(file);
		
		return session.getServletContext().getContextPath() +PathContants.PROJECT_PATH_CERT_IMPORTED+File.separator+targetFileName;
	}
	
	
	/**
	 * 创建出入库清单打印
	 * @param workMap
	 */
	public void createStoragePDF(Map<String,Object> rMap){
		logger.info("createStoragePDF########start");
		/* 1.模板（空文件）路径 */
		String tempPath = "../../pdf_template/";
		/* 2.模板（文件）名称 */
		String tempFileName = null;
		/* 3.目标文件路径 */
		
		String targetPath = (String)rMap.get("credentialPath");
		/* 4.目标文件名称 */
		String targetFileName = null;
		
		String type = (String)rMap.get("credential_type");
		logger.debug("type===>"+type);
		if ("".equals(type) || null == type) {
			logger.error("凭证参数为空！");
			// throw new exception();
		}
		
		//20180524 惠国磊  以下  行   
		//添加库存打印模板
		if("storage".equals(type)){
			tempFileName = STORAGE_TEMP_1+".pdf";
		}
		String storageNo = (String)rMap.get("storageNo");
		targetFileName = storageNo+"_print.pdf";
		if(logger.isDebugEnabled()){
			logger.debug("targetFileName:"+targetFileName);
		}
		
		StorageTicket ticket = new StorageTicket();
		ticket.setAmount(Integer.parseInt((String)rMap.get("totalAmount")));
		ticket.setTitle((String)rMap.get("title"));
		ticket.setBranchNo((String)rMap.get("branchNo"));
		ticket.setStorageNo(storageNo);
		ticket.setOperateDate((String)rMap.get("curDate"));
		
		//装载卡产品列表
		List<String> cardPList = (List<String>) rMap.get("cardPNameList");
		//装载制卡号列表
		List<String> makeCardNoList = null;
		if(!"".equals(rMap.get("companyList"))&&rMap.get("companyList")!=null){
			makeCardNoList = (List<String>) rMap.get("makeCardNoList");
		}
		
		List<String> companyList = null;
		//装载制卡商/来源列表
		if(!"".equals(rMap.get("companyList"))&&rMap.get("companyList")!=null){
			companyList = (List<String>) rMap.get("companyList");
		}		
		//装在对应的数量
		List<Integer> amountList = (List<Integer>) rMap.get("amountList");
		
		ticket.setMakeCardNoList(makeCardNoList);
		ticket.setCardPNameList(cardPList);
		ticket.setCompanyList(companyList);
		ticket.setSubAmountList(amountList);
		logger.debug("张票的内容===>"+ticket);
		
		PDFTempletTicket pdfTT = new PDFTempletTicket();
		
		// 指定服务器路径，pdf文件的名称定义为每个流程凭证+是市民卡ha
		pdfTT.setTemplatePdfPath(tempPath+tempFileName);

		// 打印机上的服务
		pdfTT.setTargetPdfpath(targetPath+File.separator+targetFileName);
		logger.debug("pdfTT_path:"+pdfTT.getTargetPdfpath());
		pdfTT.setStorageTicket(ticket);
		
		//创建远程pdf文件
		//创建文件夹20190222 hgl
		File dir = new File(targetPath);
		if(!dir.exists()){
			dir.mkdir();
		}
		File file = new File(targetPath+File.separator+targetFileName);
		
		pdfTT.templetStorageTicket(file);
		
		
		logger.info("createStoragePDF########end");
	}
	
}
